<script>
import VueResource from 'vue-resource'
import md5 from 'js-md5';
import {c_article_share_count, c_wechat_config} from '../config/api'
import wx from 'weixin-js-sdk'

var rnd = '', timestamp = '', title = " - 有喜财富手机版", nodateStr="没有数据了", loading=true;
var show_loading = false, g_counting=null;




function r_key() {
  rnd = Math.random().toString(36).substr(2);
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  //timestamp = gettime();
  var url = rnd
  //var rnt = (timestamp+url).substring(7, 18)
  return timestamp+url
}

function p_key() {
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;


  var url = rnd;
  var rnt = (timestamp+url).substring(7, 18)
  //return timestamp+url
  //return md5(rnt)
  return md5(md5(rnt)+md5(url))

}



function gettime() {

    var xhr = null;
    if(window.XMLHttpRequest){
      xhr = new window.XMLHttpRequest();
    }else{ // ie
      xhr = new ActiveObject("Microsoft")
    }
    xhr.open("GET","/credential.html",false)
    xhr.send(null);
    var date = xhr.getResponseHeader("Date");
    var timestamp2 = Date.parse(new Date(date));
    timestamp2 = timestamp2 / 1000;
    return timestamp2
}

function wxset(wx_share) {

    var xhr = null;
    if(window.XMLHttpRequest){
      xhr = new window.XMLHttpRequest();
    }else{ // ie
      xhr = new ActiveObject("Microsoft")
    }
    var data = {url :  window.location.href.split('#')[0] };

    console.log('link:'+wx_share.link)
    console.log('location:'+window.location.href)

    xhr.onreadystatechange = function(){
      if(xhr.readyState==4 && xhr.status==200){
        var data =  JSON.parse(xhr.response)

        wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: 'wx70e20b89b7fc9621', // 必填，公众号的唯一标识
          timestamp:data.timestamp, // 必填，生成签名的时间戳
          nonceStr: data.noncestr, // 必填，生成签名的随机串
          signature: data.signature,// 必填，签名，见附录1
          jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        });

        wx.ready(function () {

          wx.onMenuShareAppMessage({
            title: wx_share.title, // 分享标题
            desc: wx_share.desc, // 分享描述
            link: wx_share.link || window.location.href, // 分享链接
            imgUrl: wx_share.img, // 分享图标
            //type: '', // 分享类型,music、video或link，不填默认为link
            //dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
              if(wx_share.newsID!=undefined)
                addView(wx_share.newsID)
              // 用户确认分享后执行的回调函数
              //if(that.$route.name=="detail") that.addView();
            }/*,
            cancel: function () {
              //console.log(this)
              if(wx_share.newsID!=undefined)
                addView(wx_share.newsID)
            // 用户取消分享后执行的回调函数
              //if(that.$route.name=="detail") that.addView();
            }*/
          });


          wx.onMenuShareTimeline({
            title: wx_share.title, // 分享标题
            link: wx_share.link || window.location.href, // 分享链接
            imgUrl: wx_share.img, // 分享图标
            success: function () {
              if(wx_share.newsID!=undefined)
                addView(wx_share.newsID)
              //addView()
              //if(that.$route.name=="detail") that.addView();
              // 用户确认分享后执行的回调函数
            }/*,
            cancel: function () {
            // 用户取消分享后执行的回调函数
            }*/
          });

        });
      }
    }

    xhr.open("POST",c_wechat_config,true)
    if(typeof FormData == "undefined") {
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    xhr.send(postDataFormat(data));
    //xhr.send('url='+wx_share.link);
}

function addView(id){


    var xhr = null;
    if(window.XMLHttpRequest){
      xhr = new window.XMLHttpRequest();
    }else{ // ie
      xhr = new ActiveObject("Microsoft")
    }
    xhr.open("GET",c_article_share_count+'&article_id='+id,false)
    xhr.send(null);
}

function postDataFormat(obj){
  if(typeof obj != "object" ) {
  alert("输入的参数必须是对象");
  return;
  }

  // 支持有FormData的浏览器（Firefox 4+ , Safari 5+, Chrome和Android 3+版的Webkit）
  if(typeof FormData == "function") {
  var data = new FormData();
  for(var attr in obj) {
  data.append(attr,obj[attr]);
  }
  return data;
  }else {
  // 不支持FormData的浏览器的处理
  var arr = new Array();
  var i = 0;
  for(var attr in obj) {
  arr[i] = encodeURIComponent(attr) + "=" + encodeURIComponent(obj[attr]);
  i++;
  }
  return arr.join("&");
  }
}


function clear_key() {
  clearInterval(g_counting);
}



export default
{
  r_key,
  p_key,
  show_loading,
  nodateStr,
  loading,
  title,
  wxset
}
</script>
